﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace EncircleFTM
{
    public partial class AdditionalInformation : Form
    {
        #region "Member variables"
        ListBox listBoxProvider;
        ListBox listBoxProcedure1;
        ListBox listBoxProcedure2;
        ListBox listBoxNote1;
        ListBox listBoxNote2;
        #endregion

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        public const int SWP_NOMOVE = 0x0002;
        public const int SWP_NOSIZE = 0x0001;
        public const int HWND_TOPMOST = -1;

        #region"Constructor"
        /// <summary>
        /// Onload binding Controls
        /// </summary>
        public AdditionalInformation()
        {
            InitializeComponent();
            this.TopLevel = true;
            this.TopMost = true;
            txtPatientID.Focus();

            listBoxProvider = new ListBox();
            listBoxProvider.Visible = false;
            Controls.Add(listBoxProvider);

            listBoxProcedure1 = new ListBox();
            listBoxProcedure1.Visible = false;
            Controls.Add(listBoxProcedure1);

            listBoxProcedure2 = new ListBox();
            listBoxProcedure2.Visible = false;
            Controls.Add(listBoxProcedure2);


            listBoxNote1 = new ListBox();
            listBoxNote1.Visible = false;
            Controls.Add(listBoxNote1);


            listBoxNote2 = new ListBox();
            listBoxNote2.Visible = false;
            Controls.Add(listBoxNote2);
        }
        #endregion
        #region"Events"
        ///// <summary>
        ///// Update Static list
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void chkProvider_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkProvider.Checked == true)
        //    {
        //        bool updationstatus = clsSettings.UpdateStaticListInfo(txtProvider.Text, clsCommonData.STR_STATIC_PROVIDER);
        //    }
        //}
        ///// <summary>
        ///// Update Static list
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void chkProcedure1_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkProcedure1.Checked == true)
        //    {
        //        bool updationstatus = clsSettings.UpdateStaticListInfo(txtProcedure1.Text, clsCommonData.STR_STATIC_PROCEDURE1);
        //    }
        //}
        ///// <summary>
        ///// Update Static list
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void chkProcedure2_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkProcedure2.Checked == true)
        //    {
        //        bool updationstatus = clsSettings.UpdateStaticListInfo(txtProcedure2.Text, clsCommonData.STR_STATIC_PROCEDURE2);
        //    }
        //}
        ///// <summary>
        ///// Update Static list
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void chkNote1_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkNote1.Checked == true)
        //    {
        //        bool updationstatus = clsSettings.UpdateStaticListInfo(txtNote1.Text, clsCommonData.STR_STATIC_NOTE1);
        //    }
        //}
        ///// <summary>
        ///// Update Static list
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void chkNote2_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (chkNote2.Checked == true)
        //    {
        //        bool updationstatus = clsSettings.UpdateStaticListInfo(txtNote2.Text, clsCommonData.STR_STATIC_NOTE2);
        //    }
        //}
        ///// <summary>
        ///// Read patienID from XML
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        private void btnRecentPatientID_Click(object sender, EventArgs e)
        {
            //read XML information for Most recent patient ID used 
            string MostRecentPatientID = string.Empty;
            MostRecentPatientID = clsSettings.ReadStaticListInfo();
            if (MostRecentPatientID != string.Empty)
            {
                txtPatientID.Text = MostRecentPatientID;
            }
            else
            {
                lblStatus.Text = "No Patient ID Exists";
            }
        }
        /// <summary>
        /// append additional information to Server Data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string patient_id, DocumentType, Provider, Proceudure1, Procedure2, Note1, Note2;
            DateTime servicedate = Convert.ToDateTime(dtServiceDate.Text);
            patient_id = DocumentType = Provider = Proceudure1 = Procedure2 = Note1 = Note2 = string.Empty;
            try
            {
                if (txtPatientID.Text != string.Empty)
                {
                    patient_id = txtPatientID.Text;
                }
                if (cmbDocumentType.SelectedItem != null)
                {
                    DocumentType = cmbDocumentType.SelectedText;
                }
                if (txtProvider.Text != string.Empty)
                {
                    Provider = txtProvider.Text;
                }
                if (txtProcedure1.Text != string.Empty)
                {
                    Proceudure1 = txtProcedure1.Text;
                }
                if (txtProcedure2.Text != string.Empty)
                {
                    Procedure2 = txtProcedure2.Text;
                }
                if (txtNote1.Text != string.Empty)
                {
                    Note1 = txtNote1.Text;
                }
                if (txtNote2.Text != string.Empty)
                {
                    Note2 = txtNote2.Text;
                }
                bool updatestatus = clsSettings.UpdateAdditionalInfo(patient_id, servicedate, DocumentType, Provider, Proceudure1, Procedure2, Note1, Note2);
                if (!updatestatus)
                {
                    lblStatus.Text = "Updation of additional information failed";
                }
                else
                {
                    Updatestaticlist();
                    clsSettings.UpdateStaticListInfo(txtPatientID.Text);
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message.ToString();
                const int MAX_RETRY = 10;
                const int DELAY_MS = 1000; // 1 second
                bool result = false;
                int retry = 0;
                bool keepRetry = true;
                while (keepRetry && !result && retry < MAX_RETRY)
                {
                    try
                    {
                        bool updatestatus = clsSettings.UpdateAdditionalInfo(patient_id, servicedate, DocumentType, Provider, Proceudure1, Procedure2, Note1, Note2);
                        if (!updatestatus)
                        {
                            lblStatus.Text = "Updation of additional information failed";
                        }
                        else
                        {
                            Updatestaticlist();
                            clsSettings.UpdateStaticListInfo(txtPatientID.Text);
                            this.Close();
                        }
                    }
                    catch (System.IO.IOException ioException)
                    {
                        System.Threading.Thread.Sleep(DELAY_MS);
                        retry++;
                    }
                    catch (Exception ioex)
                    {
                        keepRetry = false;
                    }

                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFormClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void txtProvider_KeyUp(object sender, KeyEventArgs e)
        //{
        //    var x = txtProvider.Left;
        //    var y = txtProvider.Top + txtProvider.Height;
        //    var width = txtProvider.Width + 20;
        //    const int height = 40;

        //    listBoxProvider.SetBounds(x, y, width, height);
        //    listBoxProvider.KeyDown += listBoxProvider_SelectedIndexChanged;
        //    List<string> list = new List<string>();

        //    string str_Provider = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROVIDER);
        //    string[] strProviderlist = str_Provider.Split(',');
        //    foreach (string strProvider in strProviderlist)
        //    {
        //        list.Add(strProvider);

        //    }


        //    List<string> localList = list.Where(z => z.StartsWith(txtProvider.Text)).ToList();
        //    if (localList.Any() && !string.IsNullOrEmpty(txtProvider.Text))
        //    {
        //        listBoxProvider.DataSource = localList;
        //        listBoxProvider.Visible = true;
        //        listBoxProvider.BringToFront();
        //        listBoxProvider.Show();

        //    }
        //    else
        //    {
        //        listBoxProvider.Visible = false;
        //    }

        //}

        //void listBoxProvider_SelectedIndexChanged(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == (decimal)Keys.Enter)
        //    {
        //        txtProvider.Text = ((ListBox)sender).SelectedItem.ToString();

        //        listBoxProvider.Hide();
        //    }


        //}
        private void txtProvider_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Down != e.KeyData && Keys.Up != e.KeyData && e.KeyData != Keys.Tab && e.KeyValue != 13)
            {
                e.Handled = !isLetterOrDigit((char)e.KeyValue);
                var x = txtProvider.Left;
                var y = txtProvider.Top + txtProvider.Height;
                var width = txtProvider.Width + 20;
                const int height = 50;

                listBoxProvider.SetBounds(x, y, width, height);
                listBoxProvider.KeyDown += listBoxProvider_SelectedIndexChanged;
                listBoxProvider.TabStop = false;
                listBoxProvider.MouseDown += listBoxProvider_MouseDown;
                List<string> list = new List<string>();

                string str_Provider = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROVIDER);
                string[] str_Providerlist = str_Provider.Split('~');
                foreach (string strProcedure in str_Providerlist)
                {
                    list.Add(strProcedure);

                }

                List<string> localList = list.Where(z => z.ToLower().StartsWith(txtProvider.Text.ToLower())).ToList();
                if (localList.Any() && !string.IsNullOrEmpty(txtProvider.Text))
                {
                    listBoxProvider.DataSource = localList;
                    listBoxProvider.Visible = true;
                    listBoxProvider.BringToFront();
                    listBoxProvider.Show();
                }
                else
                {
                    listBoxProvider.Visible = false;
                }
            }
            else
            {
                if (e.KeyData == Keys.Down)
                {
                    if (listBoxProvider.SelectedIndex < listBoxProvider.Items.Count - 1)
                    {
                        listBoxProvider.SelectedIndex += 1;
                    }
                }
                if (e.KeyData == Keys.Up)
                {
                    if (listBoxProvider.SelectedIndex > 0)
                    {
                        listBoxProvider.SelectedIndex -= 1;
                    }

                }
                if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                {
                    if (e.KeyData == Keys.Enter)
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    if (e.KeyData == Keys.Tab)
                    {
                        listBoxProvider.Visible = false;
                    }
                    if (listBoxProvider.SelectedIndex > -1)
                    {
                        txtProvider.Text = listBoxProvider.Items[listBoxProvider.SelectedIndex].ToString();
                        txtProvider.SelectionStart = txtProvider.Text.Length;
                    }
                    listBoxProvider.Visible = false;
                }
            }

        }
        void listBoxProvider_SelectedIndexChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (decimal)Keys.Enter)
            {
                txtProvider.Text = ((ListBox)sender).SelectedItem.ToString();

                listBoxProvider.Hide();
            }
        }
        private void listBoxProvider_MouseDown(object sender, MouseEventArgs e)
        {
            txtProvider.Text = listBoxProvider.Items[listBoxProvider.SelectedIndex].ToString();
            txtProvider.SelectionStart = txtProvider.Text.Length;
            listBoxProvider.Visible = false;
        }


        //private void txtProcedure1_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    e.Handled = !isLetterOrDigit(e.KeyChar);
        //    if (!e.Handled)
        //    {
        //        var x = txtProcedure1.Left;
        //        var y = txtProcedure1.Top + txtProcedure1.Height;
        //        var width = txtProvider.Width + 20;
        //        const int height = 40;

        //        listBoxProcedure1.SetBounds(x, y, width, height);
        //        listBoxProcedure1.KeyDown += listBoxProcedure1_SelectedIndexChanged;
        //        List<string> list = new List<string>();

        //        string Procedure1List = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROCEDURE1);
        //        string[] strProcedure1List = Procedure1List.Split(',');
        //        foreach (string strProcedure1 in strProcedure1List)
        //        {
        //            list.Add(strProcedure1);

        //        }

        //        List<string> localList = list.Where(z => z.StartsWith(txtProcedure1.Text)).ToList();
        //        if (localList.Any() && !string.IsNullOrEmpty(txtProcedure1.Text))
        //        {
        //            listBoxProcedure1.DataSource = localList;
        //            listBoxProcedure1.Visible = true;
        //            listBoxProcedure1.BringToFront();
        //            listBoxProcedure1.Show();

        //        }
        //        else
        //        {
        //            listBoxProcedure1.Visible = false;
        //        }
        //    }
        //}
        //void listBoxProcedure1_SelectedIndexChanged(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == (decimal)Keys.Enter)
        //    {
        //        txtProcedure1.Text = ((ListBox)sender).SelectedItem.ToString();
        //        listBoxProcedure1.Hide();
        //    }
        //}       
        private void txtProcedure1_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Down != e.KeyData && Keys.Up != e.KeyData && e.KeyData != Keys.Tab && e.KeyValue != 13)
            {
                e.Handled = !isLetterOrDigit((char)e.KeyValue);
                var x = txtProcedure1.Left;
                var y = txtProcedure1.Top + txtProcedure1.Height;
                var width = txtProcedure1.Width + 20;
                const int height = 50;

                listBoxProcedure1.SetBounds(x, y, width, height);
                listBoxProcedure1.KeyDown += listBoxProcedure1_SelectedIndexChanged;
                listBoxProcedure1.TabStop = false;
                listBoxProcedure1.MouseDown += listBoxProcedure1_MouseDown;
                List<string> list = new List<string>();

                string str_Procedure1 = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROCEDURE1);
                string[] str_Procedure1list = str_Procedure1.Split('~');
                foreach (string strProcedure in str_Procedure1list)
                {
                    list.Add(strProcedure);

                }

                List<string> localList = list.Where(z => z.ToLower().StartsWith(txtProcedure1.Text.ToLower())).ToList();
                if (localList.Any() && !string.IsNullOrEmpty(txtProcedure1.Text))
                {
                    listBoxProcedure1.DataSource = localList;
                    listBoxProcedure1.Visible = true;
                    listBoxProcedure1.BringToFront();
                    listBoxProcedure1.Show();
                }
                else
                {
                    listBoxProcedure1.Visible = false;
                }
            }
            else
            {
                if (e.KeyData == Keys.Down)
                {
                    if (listBoxProcedure1.SelectedIndex < listBoxProcedure1.Items.Count - 1)
                    {
                        listBoxProcedure1.SelectedIndex += 1;
                    }
                }
                if (e.KeyData == Keys.Up)
                {
                    if (listBoxProcedure1.SelectedIndex > 0)
                    {
                        listBoxProcedure1.SelectedIndex -= 1;
                    }

                }
                if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                {
                    if (e.KeyData == Keys.Enter)
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    if (e.KeyData == Keys.Tab)
                    {
                        listBoxProcedure1.Visible = false;
                    }
                    if (listBoxProcedure1.SelectedIndex > -1)
                    {
                        txtProcedure1.Text = listBoxProcedure1.Items[listBoxProcedure1.SelectedIndex].ToString();
                        txtProcedure1.SelectionStart = txtProcedure1.Text.Length;
                    }
                    listBoxProcedure1.Visible = false;
                }
            }
        }
        void listBoxProcedure1_SelectedIndexChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (decimal)Keys.Enter)
            {
                txtProcedure1.Text = ((ListBox)sender).SelectedItem.ToString();

                listBoxProcedure1.Hide();
            }
        }
        private void listBoxProcedure1_MouseDown(object sender, MouseEventArgs e)
        {
            txtProcedure1.Text = listBoxProcedure1.Items[listBoxProcedure1.SelectedIndex].ToString();
            txtProcedure1.SelectionStart = txtProcedure1.Text.Length;
            listBoxProcedure1.Visible = false;
        }


        //private void txtProcedure2_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    e.Handled = !isLetterOrDigit(e.KeyChar);
        //    if (!e.Handled)
        //    {
        //        var x = txtProcedure2.Left;
        //        var y = txtProcedure2.Top + txtProcedure2.Height;
        //        var width = txtProcedure2.Width + 20;
        //        const int height = 40;

        //        listBoxProcedure2.SetBounds(x, y, width, height);
        //        listBoxProcedure2.KeyDown += listBoxProcedure2_SelectedIndexChanged;
        //        List<string> list = new List<string>();

        //        string Procedure2List = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROCEDURE2);
        //        string[] strProcedure2List = Procedure2List.Split(',');
        //        foreach (string strProcedure2 in strProcedure2List)
        //        {
        //            list.Add(strProcedure2);

        //        }

        //        List<string> localList = list.Where(z => z.StartsWith(txtProcedure1.Text)).ToList();
        //        if (localList.Any() && !string.IsNullOrEmpty(txtProcedure2.Text))
        //        {
        //            listBoxProcedure2.DataSource = localList;
        //            listBoxProcedure2.Visible = true;
        //            listBoxProcedure2.BringToFront();
        //            listBoxProcedure2.Show();

        //        }
        //        else
        //        {
        //            listBoxProcedure2.Visible = false;
        //        }
        //    }
        //}
        //void listBoxProcedure2_SelectedIndexChanged(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == (decimal)Keys.Enter)
        //    {
        //        txtProcedure2.Text = ((ListBox)sender).SelectedItem.ToString();
        //        listBoxProcedure2.Hide();
        //    }


        //}
        private void txtProcedure2_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Down != e.KeyData && Keys.Up != e.KeyData && e.KeyData != Keys.Tab && e.KeyValue != 13)
            {
                e.Handled = !isLetterOrDigit((char)e.KeyValue);
                var x = txtProcedure2.Left;
                var y = txtProcedure2.Top + txtProcedure2.Height;
                var width = txtProcedure2.Width + 20;
                const int height = 50;

                listBoxProcedure2.SetBounds(x, y, width, height);
                listBoxProcedure2.KeyDown += listBoxProcedure2_SelectedIndexChanged;
                listBoxProcedure2.TabStop = false;
                listBoxProcedure2.MouseDown += listBoxProcedure2_MouseDown;
                List<string> list = new List<string>();

                string str_Procedure2 = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_PROCEDURE2);
                string[] str_Procedure2list = str_Procedure2.Split('~');
                foreach (string strProcedure in str_Procedure2list)
                {
                    list.Add(strProcedure);

                }

                List<string> localList = list.Where(z => z.ToLower().StartsWith(txtProcedure2.Text.ToLower())).ToList();
                if (localList.Any() && !string.IsNullOrEmpty(txtProcedure2.Text))
                {
                    listBoxProcedure2.DataSource = localList;
                    listBoxProcedure2.Visible = true;
                    listBoxProcedure2.BringToFront();
                    listBoxProcedure2.Show();
                }
                else
                {
                    listBoxProcedure2.Visible = false;
                }
            }
            else
            {
                if (e.KeyData == Keys.Down)
                {
                    if (listBoxProcedure2.SelectedIndex < listBoxProcedure2.Items.Count - 1)
                    {
                        listBoxProcedure2.SelectedIndex += 1;
                    }
                }
                if (e.KeyData == Keys.Up)
                {
                    if (listBoxProcedure2.SelectedIndex > 0)
                    {
                        listBoxProcedure2.SelectedIndex -= 1;
                    }

                }
                if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                {
                    if (e.KeyData == Keys.Enter)
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    if (e.KeyData == Keys.Tab)
                    {
                        listBoxProcedure2.Visible = false;
                    }
                    if (listBoxProcedure2.SelectedIndex > -1)
                    {
                        txtProcedure2.Text = listBoxProcedure2.Items[listBoxProcedure2.SelectedIndex].ToString();
                        txtProcedure2.SelectionStart = txtProcedure2.Text.Length;
                    }
                    listBoxProcedure2.Visible = false;
                }
            }
        }
        void listBoxProcedure2_SelectedIndexChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (decimal)Keys.Enter)
            {
                txtProcedure2.Text = ((ListBox)sender).SelectedItem.ToString();

                listBoxProcedure2.Hide();
            }
        }
        private void listBoxProcedure2_MouseDown(object sender, MouseEventArgs e)
        {
            txtProcedure2.Text = listBoxProcedure2.Items[listBoxProcedure2.SelectedIndex].ToString();
            txtProcedure2.SelectionStart = txtProcedure2.Text.Length;
            listBoxProcedure2.Visible = false;
        }

        //void listBoxNote1_SelectedIndexChanged(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == (decimal)Keys.Enter)
        //    {
        //        txtNote1.Text = ((ListBox)sender).SelectedItem.ToString();
        //        listBoxNote1.Hide();
        //    }
        //}
        //private void txtNote1_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    e.Handled = !isLetterOrDigit(e.KeyChar);
        //    if (!e.Handled)
        //    {
        //        var x = txtNote1.Left;
        //        var y = txtNote1.Top + txtNote1.Height;
        //        var width = txtNote1.Width + 20;
        //        const int height = 40;

        //        listBoxNote1.SetBounds(x, y, width, height);
        //        listBoxNote1.KeyDown += listBoxNote1_SelectedIndexChanged;
        //        List<string> list = new List<string>();

        //        string Note1List = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_NOTE1);
        //        string[] strNote1List = Note1List.Split(',');
        //        foreach (string strNote1 in strNote1List)
        //        {
        //            list.Add(strNote1);

        //        }

        //        List<string> localList = list.Where(z => z.StartsWith(txtNote1.Text)).ToList();
        //        if (localList.Any() && !string.IsNullOrEmpty(txtNote1.Text))
        //        {
        //            listBoxNote1.DataSource = localList;
        //            listBoxNote1.Visible = true;
        //            listBoxNote1.BringToFront();
        //            listBoxNote1.Show();

        //        }
        //        else
        //        {
        //            listBoxNote1.Visible = false;
        //        }
        //    }
        //}
        private void txtNote1_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Down != e.KeyData && Keys.Up != e.KeyData && e.KeyData != Keys.Tab && e.KeyValue != 13)
            {
                e.Handled = !isLetterOrDigit((char)e.KeyValue);
                var x = txtNote1.Left;
                var y = txtNote1.Top + txtNote1.Height;
                var width = txtNote1.Width + 20;
                const int height = 50;

                listBoxNote1.SetBounds(x, y, width, height);
                listBoxNote1.KeyDown += listBoxNote1_SelectedIndexChanged;
                listBoxNote1.TabStop = false;
                listBoxNote1.MouseDown += listBoxNote1_MouseDown;
                List<string> list = new List<string>();

                string str_Note1 = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_NOTE1);
                string[] str_Note1list = str_Note1.Split('~');
                foreach (string strProcedure in str_Note1list)
                {
                    list.Add(strProcedure);

                }

                List<string> localList = list.Where(z => z.ToLower().StartsWith(txtNote1.Text.ToLower())).ToList();
                if (localList.Any() && !string.IsNullOrEmpty(txtNote1.Text))
                {
                    listBoxNote1.DataSource = localList;
                    listBoxNote1.Visible = true;
                    listBoxNote1.BringToFront();
                    listBoxNote1.Show();
                }
                else
                {
                    listBoxNote1.Visible = false;
                }
            }
            else
            {
                if (e.KeyData == Keys.Down)
                {
                    if (listBoxNote1.SelectedIndex < listBoxNote1.Items.Count - 1)
                    {
                        listBoxNote1.SelectedIndex += 1;
                    }
                }
                if (e.KeyData == Keys.Up)
                {
                    if (listBoxNote1.SelectedIndex > 0)
                    {
                        listBoxNote1.SelectedIndex -= 1;
                    }

                }
                if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                {
                    if (e.KeyData == Keys.Enter)
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    if (e.KeyData == Keys.Tab)
                    {
                        listBoxNote1.Visible = false;
                    }
                    if (listBoxNote1.SelectedIndex > -1)
                    {
                        txtNote1.Text = listBoxNote1.Items[listBoxNote1.SelectedIndex].ToString();
                        txtNote1.SelectionStart = txtNote1.Text.Length;
                    }
                    listBoxNote1.Visible = false;
                }
            }
        }
        void listBoxNote1_SelectedIndexChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (decimal)Keys.Enter)
            {
                txtNote1.Text = ((ListBox)sender).SelectedItem.ToString();

                listBoxNote1.Hide();
            }
        }
        private void listBoxNote1_MouseDown(object sender, MouseEventArgs e)
        {
            txtNote1.Text = listBoxNote1.Items[listBoxNote1.SelectedIndex].ToString();
            txtNote1.SelectionStart = txtNote1.Text.Length;
            listBoxNote1.Visible = false;
        }

        //private void txtNote2_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    e.Handled = !isLetterOrDigit(e.KeyChar);
        //    if (!e.Handled)
        //    {
        //        var x = txtNote2.Left;
        //        var y = txtNote2.Top + txtNote2.Height;
        //        var width = txtNote2.Width + 20;
        //        const int height = 40;

        //        listBoxNote2.SetBounds(x, y, width, height);
        //        listBoxNote2.KeyDown += listBoxNote2_SelectedIndexChanged;
        //        List<string> list = new List<string>();

        //        string Note2List = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_NOTE2);
        //        string[] strNote2List = Note2List.Split(',');
        //        foreach (string strNote2 in strNote2List)
        //        {
        //            list.Add(strNote2);

        //        }

        //        List<string> localList = list.Where(z => z.StartsWith(txtNote2.Text)).ToList();
        //        if (localList.Any() && !string.IsNullOrEmpty(txtNote2.Text))
        //        {
        //            listBoxNote2.DataSource = localList;
        //            listBoxNote2.Visible = true;
        //            listBoxNote2.BringToFront();
        //            listBoxNote2.Show();

        //        }
        //        else
        //        {
        //            listBoxNote2.Visible = false;
        //        }
        //    }
        //}
        //void listBoxNote2_SelectedIndexChanged(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == (decimal)Keys.Enter)
        //    {
        //        txtNote2.Text = ((ListBox)sender).SelectedItem.ToString();
        //        listBoxNote2.Hide();

        //    }


        //}
        private void txtNote2_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Down != e.KeyData && Keys.Up != e.KeyData && e.KeyData != Keys.Tab && e.KeyValue != 13)
            {
                e.Handled = !isLetterOrDigit((char)e.KeyValue);
                var x = txtNote2.Left;
                var y = txtNote2.Top + txtNote2.Height;
                var width = txtNote2.Width + 20;
                const int height = 50;

                listBoxNote2.SetBounds(x, y, width, height);
                listBoxNote2.KeyDown += listBoxNote2_SelectedIndexChanged;
                listBoxNote2.TabStop = false;
                listBoxNote2.MouseDown += listBoxNote2_MouseDown;
                List<string> list = new List<string>();

                string str_Note2 = clsSettings.ReadStaticListInfo(clsCommonData.STR_STATIC_NOTE2);
                string[] str_Note2list = str_Note2.Split('~');
                foreach (string strProcedure in str_Note2list)
                {
                    list.Add(strProcedure);

                }

                List<string> localList = list.Where(z => z.ToLower().StartsWith(txtNote2.Text.ToLower())).ToList();
                if (localList.Any() && !string.IsNullOrEmpty(txtNote2.Text))
                {
                    listBoxNote2.DataSource = localList;
                    listBoxNote2.Visible = true;
                    listBoxNote2.BringToFront();
                    listBoxNote2.Show();
                }
                else
                {
                    listBoxNote2.Visible = false;
                }
            }
            else
            {
                if (e.KeyData == Keys.Down)
                {
                    if (listBoxNote2.SelectedIndex < listBoxNote2.Items.Count - 1)
                    {
                        listBoxNote2.SelectedIndex += 1;
                    }
                }
                if (e.KeyData == Keys.Up)
                {
                    if (listBoxNote2.SelectedIndex > 0)
                    {
                        listBoxNote2.SelectedIndex -= 1;
                    }

                }
                if (e.KeyData == Keys.Tab || e.KeyData == Keys.Enter)
                {
                    if (e.KeyData == Keys.Enter)
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                    if (e.KeyData == Keys.Tab)
                    {
                        listBoxNote2.Visible = false;
                    }
                    if (listBoxNote2.SelectedIndex > -1)
                    {
                        txtNote2.Text = listBoxNote2.Items[listBoxNote2.SelectedIndex].ToString();
                        txtNote2.SelectionStart = txtNote2.Text.Length;
                    }
                    listBoxNote2.Visible = false;
                }
            }
        }
        void listBoxNote2_SelectedIndexChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (decimal)Keys.Enter)
            {
                txtNote2.Text = ((ListBox)sender).SelectedItem.ToString();

                listBoxNote2.Hide();
            }
        }
        private void listBoxNote2_MouseDown(object sender, MouseEventArgs e)
        {
            txtNote2.Text = listBoxNote2.Items[listBoxNote2.SelectedIndex].ToString();
            txtNote2.SelectionStart = txtNote2.Text.Length;
            listBoxNote2.Visible = false;
        }


        /// <summary>
        /// Fetch Recent ID from the XML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPatientID_Click(object sender, EventArgs e)
        {
            //read XML information for Most recent patient ID used 
            string MostRecentPatientID = string.Empty;
            MostRecentPatientID = clsSettings.ReadStaticListInfo();
            if (MostRecentPatientID != string.Empty)
            {
                txtPatientID.Text = MostRecentPatientID;
            }
            else
            {
                lblStatus.Text = "No Patient ID Exists";
            }
        }

        private void txtPatientID_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !isLetterOrDigit(e.KeyChar);
        }

        private bool isLetterOrDigit(Char ch)
        {
            return (Char.IsLetterOrDigit(ch) || ch == '\b' || Char.IsWhiteSpace(ch));
        }

        private void AdditionalInformation_Shown(object sender, EventArgs e)
        {
            txtPatientID.Focus();
            this.TopLevel = true;
            this.TopMost = true;
            this.Activate();
            this.Focus(); 
        }

        private void AdditionalInformation_Activated(object sender, EventArgs e)
        {
            SetTopLevel(true);
            SetWindowPos(this.Handle, (IntPtr)HWND_TOPMOST, 455, 455, 455, 455, SWP_NOSIZE | SWP_NOMOVE);
            this.Activate();
            this.Focus();            
        }

        #endregion
        private bool checkValueExists(string value, string Type)
        {
            bool isExists = false;
            try
            {

                string str_staticvalue = clsSettings.ReadStaticListInfo(Type);
                string[] strStaticValuelist = str_staticvalue.Split('~');
                isExists = strStaticValuelist.Contains(value);

            }
            catch (Exception ex)
            {
                isExists = false;
            }
            return isExists;
        }
        public void Updatestaticlist()
        {


            if (chkProvider.Checked == true)
            {
                if (!checkValueExists(txtProvider.Text, clsCommonData.STR_STATIC_PROVIDER))
                {
                    bool updationstatus = clsSettings.UpdateStaticListInfo(txtProvider.Text, clsCommonData.STR_STATIC_PROVIDER);
                }
            }
            if (chkProcedure1.Checked == true)
            {
                if (!checkValueExists(txtProcedure1.Text, clsCommonData.STR_STATIC_PROCEDURE1))
                {
                    bool updationstatus = clsSettings.UpdateStaticListInfo(txtProcedure1.Text, clsCommonData.STR_STATIC_PROCEDURE1);
                }
            }
            if (chkProcedure2.Checked == true)
            {
                if (!checkValueExists(txtProcedure2.Text, clsCommonData.STR_STATIC_PROCEDURE2))
                {
                    bool updationstatus = clsSettings.UpdateStaticListInfo(txtProcedure2.Text, clsCommonData.STR_STATIC_PROCEDURE2);
                }
            }
            if (chkNote1.Checked == true)
            {
                if (!checkValueExists(txtNote1.Text, clsCommonData.STR_STATIC_NOTE1))
                {
                    bool updationstatus = clsSettings.UpdateStaticListInfo(txtNote1.Text, clsCommonData.STR_STATIC_NOTE1);
                }
            }
            if (chkNote2.Checked == true)
            {
                if (!checkValueExists(txtNote2.Text, clsCommonData.STR_STATIC_NOTE2))
                {
                    bool updationstatus = clsSettings.UpdateStaticListInfo(txtNote2.Text, clsCommonData.STR_STATIC_NOTE2);
                }
            }
        }

        private void txtProvider_Leave(object sender, EventArgs e)
        {
            hideListBox();            
        }

        private void txtProcedure1_Leave(object sender, EventArgs e)
        {
            hideListBox();            
        }

        private void txtProcedure2_Leave(object sender, EventArgs e)
        {
            hideListBox();            
        }

        private void txtNote1_Leave(object sender, EventArgs e)
        {
            hideListBox();            
        }

        private void txtNote2_Leave(object sender, EventArgs e)
        {
            hideListBox();            
        }
        void hideListBox()
        {
            //listBoxProvider.Visible = false;
            //listBoxProcedure1.Visible = false;
            //listBoxProcedure2.Visible = false;
            //listBoxNote1.Visible = false;
            //listBoxNote2.Visible = false;
        }

        private void CheckRemember(CheckBox control, TextBox txtbox)
        {
            if (control.Checked.Equals(true) && txtbox.Text.Equals(string.Empty))
            {
                MessageBox.Show("Cannot remember empty fields..!");
                control.Checked = false;
            }
        }

        private void chkProvider_Click(object sender, EventArgs e)
        {
            CheckRemember(chkProvider, txtProvider);
        }

        private void chkProcedure1_CheckedChanged(object sender, EventArgs e)
        {
            CheckRemember(chkProcedure1, txtProcedure1);
        }

        private void chkProcedure2_CheckedChanged(object sender, EventArgs e)
        {
            CheckRemember(chkProcedure2, txtProcedure2);
        }

        private void chkNote1_CheckedChanged(object sender, EventArgs e)
        {
            CheckRemember(chkNote1, txtNote1);
        }

        private void chkNote2_CheckedChanged(object sender, EventArgs e)
        {
            CheckRemember(chkNote2, txtNote2);
        }

        
    }
}
